package com.cbgolf.oa.model;

import android.support.v4.util.ArrayMap;
import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.contract.IStatisticsListDetailsContract.IStatisticsListDetailsModel;
import com.cbgolf.oa.entity.ListBean;
import com.cbgolf.oa.entity.NetResponse;
import com.cbgolf.oa.event.EventStatistics;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import java.util.Map;
import org.greenrobot.eventbus.EventBus;

/**
 * @author lq
 * @date 2018/5/7
 */

public class StatisticsListDetailsModelImp extends BaseModel implements
    IStatisticsListDetailsModel {


  @Override
  public void requestData(int dataType, int requestEvent, ListBean data) {
    if (null == data) {
      Util.show("sorry,data is null");
      return;
    }
    Map<String, String> map = new ArrayMap<>();
    String url;
    switch (dataType) {
      case DataManager.STATISTICS_BUSINESS_ALL:
      case DataManager.STATISTICS_PLAYER_ALL:
        url = WebAPI.statistics_player_info_get + data.id;
        break;
      case DataManager.STATISTICS_CADDIE_ALL:
        url = WebAPI.statistics_caddie_details_get + data.caddieId;
        map.put("day", TextUtil.Text(data.day));
        map.put("postfix", TextUtil.Text(data.postfix));
        map.put("pageSize", "30");
        map.put("currentPage", (requestEvent == LOADMORE ? String.valueOf(page++) : "0"));
        break;
      case DataManager.STATISTICS_BOOK_ALL:
        url = WebAPI.statistics_book_details_get + data.id;
        map.put("orderClassification", TextUtil.Text(data.orderClassification));
        break;
      case DataManager.STATISTICS_BILL_ALL:
        url = WebAPI.account_details_get + "/" + data.customerId;
        map.put("recordDate", data.endTime > 0 ? String.valueOf(data.endTime) : "");
        break;
      default:
        url = NetUtils.BAIDU;
        break;
    }
    if (Util.isNull(url)) {
      backResult(dataType, null, requestEvent);
      return;
    }
    Web.getOK(url, map, new NetCallBack(response -> {
      backResult(dataType, response, requestEvent);
    }));
  }

  private void backResult(int dataType, NetResponse response, int requestEvent) {
    EventBus.getDefault().post(new EventStatistics.Builder()
        .setTarget(getMsgTarget(dataType))
        .setPlayerData((ListBean) getBean(response, ListBean.class))
        .setListCaddie(getList(response, ListBean.class))
        .setRequestEvent(requestEvent)
        .setError(errorMsg, errorCode)
        .build());

  }

  private int getMsgTarget(int dataType) {
    int target = 0;
    switch (dataType) {
      case DataManager.STATISTICS_PLAYER_ALL:
        target = DataManager.STATISTICS_PLAYER_DETAILS;
        break;
      case DataManager.STATISTICS_BUSINESS_ALL:
        target = DataManager.STATISTICS_BUSINESS_DETAILS;
        break;
      case DataManager.STATISTICS_CADDIE_ALL:
        target = DataManager.STATISTICS_CADDIE_DETAILS;
        break;
      case DataManager.STATISTICS_BILL_ALL:
        target = DataManager.STATISTICS_BILL_DETAILS;
        break;
      case DataManager.STATISTICS_BOOK_ALL:
        target = DataManager.STATISTICS_BOOK_DETAILS;
        break;
      default:
        break;
    }
    return target;
  }
}
